﻿using System;
using System.Collections.Generic;
using CC_Util;
//using Facebook.Unity;
//using Facebook.Unity.Example;
using UnityEngine;
using UnityEngine.UI;

namespace Topifish.SdkAgent {
    public class C_FacebookAgent : MonoBehaviour {
        static C_FacebookAgent instance;
        public static C_FacebookAgent GetInstance {
            get {
                if (instance == null) {
                    GameObject gg = new GameObject("C_FacebookAgent");
                    MonoBehaviour.DontDestroyOnLoad(gg);
                    instance = gg.AddComponent<C_FacebookAgent>();
                }
                return instance;
            }
        }

        #region-------------初始化------------------------------------
        public void S_Init() {
            if (C_Parameter.isUse == false) {
                Debug.LogWarningFormat("facebookSdk_____未开启");
                return;
            }
            ////日志应用程序激活
            //if (FB.IsInitialized) {
            //    FB.ActivateApp();
            //    // Initialize the Facebook SDK
            //    FB.Init(S_InitCallback, S_OnHideUnity);
            //} else {
            //    //Handle FB.Init
            //    FB.Init(() => {
            //        FB.ActivateApp();
            //    });
            //}
            this.S_Delayed(1.5f).d_EndEvent = delegate () {
#if !UNITY_EDITOR
                //S_Login();
#endif
                S_logEvent_Test01();
            };
        }
        void S_InitCallback() {
            //if (FB.IsInitialized) {
            //    // Signal an app activation App Event
            //    FB.ActivateApp();
            //    // Continue with Facebook SDK
            //    // ...
            //    Debug.LogFormat("非死不可______________初始化完成");
            //} else {
            //    Debug.LogFormat("非死不可_______________未能初始化Facebook SDK");
            //}
        }

        void S_OnHideUnity(bool isGameShown) {
            if (!isGameShown) {
                // Pause the game - we will need to hide
                // Time.timeScale = 0;
                Debug.LogFormat("非死不可________________游戏暂停");
            } else {
                // Resume the game - we're getting focus again
                // Time.timeScale = 1;
                Debug.LogFormat("非死不可________________游戏继续");
            }
        }

        #endregion---------------------------------------
        void OnApplicationPause(bool pauseStatus) {
            if (C_Parameter.isUse == false) {
                Debug.LogWarningFormat("facebookSdk_____未开启");
                return;
            }
            // 日志应用程序激活
            // Check the pauseStatus to see if we are in the foreground
            // or background
            if (!pauseStatus) {
                ////app resume
                //if (FB.IsInitialized) {
                //    FB.ActivateApp();
                //} else {
                //    //Handle FB.Init
                //    FB.Init(() => {
                //        FB.ActivateApp();
                //    });
                //}
            }
        }

















        #region--------------------facebook登录------------------------------------------
        void S_Login() {
            //var perms = new List<string>() { "public_profile", "email" };
            //FB.LogInWithReadPermissions(perms, AuthCallback);
        }

        //void AuthCallback(ILoginResult result) {
        //    if (FB.IsLoggedIn) {
        //        // AccessToken class will have session details
        //        var aToken = Facebook.Unity.AccessToken.CurrentAccessToken;
        //        // Print current access token's User ID
        //        Debug.Log("facebookSdk________________UserId=" + aToken.UserId);
        //        // Print current access token's granted permissions
        //        int i = 0;
        //        foreach (string perm in aToken.Permissions) {
        //            Debug.LogFormat("facebookSdk__________{0}_____{1}_",i,perm);
        //        }
        //    } else {
        //        Debug.Log("facebookSdk________________用户取消登录");
        //    }
        //}

        #endregion---------------------------------------------------------





        #region--------------------分享给facebook----------------------
        void S_Share() {
            //FB.ShareLink(
            //    new Uri("https://developers.facebook.com/"),
            //    callback: ShareCallback
            //);
        }
        //void ShareCallback(IShareResult result) {
        //    if (result.Cancelled || !string.IsNullOrEmpty(result.Error)) {
        //        Debug.Log("ShareLink Error: " + result.Error);
        //    } else if (!string.IsNullOrEmpty(result.PostId)) {
        //        // Print post identifier of the shared content
        //        Debug.Log(result.PostId);
        //    } else {
        //        // Share succeeded without postID
        //        Debug.Log("ShareLink success!");
        //    }
        //}
        #endregion--------------------------------------




        #region--------------------------记录应用程序事件-----------------
        /// <summary>
        /// 新手教程事件
        /// </summary>
        public void S_logEvent_Test01() {
            //var tutParams = new Dictionary<string, object>();
            //tutParams[AppEventParameterName.ContentID] = "教程步骤1";
            //tutParams[AppEventParameterName.Description] = "教程的第一步，单击第一个按钮！";
            //tutParams[AppEventParameterName.Success] = "1";

            //FB.LogAppEvent(AppEventName.CompletedTutorial, parameters: tutParams);
        }


        protected void S_logEvent_Test02() {
            //var tutParams = new Dictionary<string, object>() { { AppEventParameterName.Description, "Clicked 'Log AppEvent' button" } };
            //FB.LogAppEvent(AppEventName.UnlockedAchievement, null, tutParams);
            //LogView.AddLog("You may see results showing up at https://www.facebook.com/analytics/" + FB.AppId);
        }

        #endregion--------------------------------------




        public class C_Parameter {
            public static bool isUse = true;
        }

    }

}